// pages/serviceDetail/serviceDetail.js
var app = getApp();
Page({
  data: {
    serviceId: '', // 服务ID
    serviceInfo: {}, // 服务详细信息
    markers: [] // 地图标记点
  },

  onLoad(options) {
    this.setData({ serviceId: options.id });
    this.loadServiceDetail();
  },

  // 加载服务详情
  loadServiceDetail() {
    wx.showLoading({ title: '加载中...' });
    const self = this;
    app.util.request({
    	url: 'Community/serviceDetail',
    	data: {
    		m: app.mdName,
			id:this.data.serviceId
    	},
    	success(res) {
			const data = res.data.data;
			const serviceInfo = {
				id: this.data.serviceId,
				serviceType: '上门诊疗',
				status: data.status,
				appointmentTime: data.scheduled_time,
				price: data.price || 0,
				createTime: data.createtime,
				patientName: data.patient_name,
				patientGender: data.patient_sex,
				patientAge: data.patient_age,
				connectName: data.contect_name,
				connectPhone: data.contect_phone,
				//healthCondition: '高血压、糖尿病',
				address: data.service_address,
				longitude: data.longitude,
				latitude: data.latitude,
				detailDescription: data.patient_notes
			  };
    		self.setData({
    		  serviceInfo,
    		  markers: [{
    		    id: 0,
    		    latitude: serviceInfo.latitude,
    		    longitude: serviceInfo.longitude,
    		    title: serviceInfo.address,
    		    iconPath: 'https://banyetang.oss-cn-beijing.aliyuncs.com/xiaochengxu/ic_address.png',
    		    width: 30,
    		    height: 30
    		  }]
    		});
    	}
    })
  },

  // 获取状态文本
  getStatusText(status) {
    const statusMap = {
      'pending': '待确认',
      'confirmed': '已确认',
      'completed': '已完成',
      'canceled': '已取消'
    };
    return statusMap[status] || status;
  },

  // 预览图片
  previewImage(e) {
    const url = e.currentTarget.dataset.url;
    wx.previewImage({
      current: url,
      urls: this.data.serviceInfo.images
    });
  },

  // 打开地图导航
  openMapNavigation() {
    const { latitude, longitude, address } = this.data.serviceInfo;
    wx.openLocation({
      latitude,
      longitude,
      name: address,
      address,
      scale: 18
    });
  },
// 确认服务
  confirmService(e) {
    const id = e.currentTarget.dataset.id;
	const self = this
    wx.showModal({
      title: '确认服务',
      content: '确定要接受此服务请求吗？',
      success: (res) => {
        if (res.confirm) {
          wx.showLoading({ title: '处理中...' });
          app.util.request({
          	url: 'Community/confirmOrder',
          	data: {
          		m: app.mdName,
				id:this.data.serviceId
          	},
          	success(res) {
				wx.showToast({ title: '服务已确认' });
          		this.setData({
				  'serviceInfo.status': 'confirmed'
				});
          	}
          })
        }
      }
    });
  },

  // 拒绝服务
  rejectService() {
    wx.showModal({
      title: '拒绝服务',
      content: '确定要拒绝此服务请求吗？',
      success: (res) => {
        if (res.confirm) {
          wx.showLoading({ title: '处理中...' });
          
          // 模拟API请求
          setTimeout(() => {
            wx.showToast({ title: '服务已拒绝' });
            this.setData({
              'serviceInfo.status': 'canceled'
            });
            wx.navigateBack();
          }, 800);
        }
      }
    });
  },
// 完成服务
  completeService(e) {
	const self = this
    wx.showModal({
      title: '完成服务',
      content: '确认此服务已完成？',
      success: (res) => {
        if (res.confirm) {
          wx.showLoading({ title: '处理中...' });
          
          app.util.request({
				url: 'Community/finishOrder',
				data: {
					m: app.mdName,
					id:this.data.serviceId
				},
				success(res) {
					this.setData({
					  'serviceInfo.status': 'completed'
					});
					wx.navigateBack();
				}
			})
        }
      }
    });
  },

  // 查看反馈
  viewFeedback() {
    wx.navigateTo({
      url: `/pages/feedbackDetail/feedbackDetail?serviceId=${this.data.serviceId}`
    });
  }
});